package holiday_demo;

import java.util.ArrayList;

//自定义四个学生对象，添加到集合，并遍历
public class ArrayListStudent {
    public static void main(String[] args) {
        //创建学生类集合
        ArrayList<Student> stu_list=new ArrayList<>();
        //创建学生对象
        Student one=new Student("郭靖",22);
        Student two=new Student("黄蓉",20);
        Student three=new Student("洪七公",60);
        Student tour=new Student("黄药师",50);
        //添加到集合
        stu_list.add(one);
        stu_list.add(two);
        stu_list.add(three);
        stu_list.add(tour);
        //遍历学生对象
        for (Student student : stu_list) {
            System.out.println("学生姓名：" + student.getName() + "  学生年龄：" + student.getAge());
        }
        /*---------------或者-------------
        for (int i = 0; i < stu_list.size(); i++) {
            System.out.println("学生姓名："+stu_list.get(i).getName()+"  学生年龄："+stu_list.get(i).getAge());
        }
         */
    }
}
class Student{
    private String name;
    private int age;
    public Student() {
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }
}
